package com.zzyq.pc.web.configure.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzyq.pc.common.StandardDataFormat;
import com.zzyq.pc.common.base.BaseController;
import com.zzyq.pc.common.plugin.db.PageView;
import com.zzyq.pc.utils.HttpUtils;
import com.zzyq.pc.utils.LoggerUtils;
import com.zzyq.pc.web.configure.model.Company;
import com.zzyq.pc.web.configure.service.ICompanyService;

/**
 * <p>
 * 
 * @Title: CompanyController.java
 * @Description: 团体管理
 * @author: Sywd丶吴迪
 * @date: 2017年9月5日 下午2:05:20
 * @version V1.0
 *          <p>
 */
@Controller
@RequestMapping(value = "/company/", produces = "application/json;charset=UTF-8")
public class CompanyController extends BaseController {
    @Autowired
    ICompanyService companyService;

    /**
     * 根据参数查询团体列表
     * 
     * @param pageIndex
     *            页码
     * @param pageSize
     *            页面大小
     * @param companyName
     *            团体名称
     * @param companyBrie
     *            团体简称
     */
    @ResponseBody
    @RequestMapping("query")
    public String query(HttpServletRequest httpRequest, HttpServletResponse response, Integer pageIndex,
            Integer pageSize, Company company) {
        List<Company> list = new ArrayList<>();
        StandardDataFormat format = new StandardDataFormat(CODE_ERROR, MSG_ERROR, list);
        PageView pageView = null;
        try {
            // 设置分页参数
            if (pageIndex == null || pageIndex <= 0) {
                pageIndex = 1;
            }
            if (pageSize == null || pageSize <= 0) {
                pageSize = 10;
            }
            pageView = new PageView(pageSize, pageIndex);
            // 字符转换
            if (company == null) {
                company = new Company();
            }
            company.setCompanyName(transString(httpRequest, company.getCompanyName()));
            company.setCompanyBrie(transString(httpRequest, company.getCompanyBrie()));
            // 开始根据参数查询
            list = companyService.selectAsList(pageView, company);
            // 加入页码和总数
            format.setPageIndex(pageView.getPageNow());
            format.setPageSize(pageView.getPageSize());
            format.setTotalPages(pageView.getPageCount());
            format.setTotalRecords(pageView.getRowCount());
            return HttpUtils.formatData(format, CODE_OK, MSG_OK, list);
        } catch (Exception e) {
            LoggerUtils.fmtError(getClass(), "查询团体列表异常%s", e.getMessage());
            return HttpUtils.formatData(format, CODE_EXCEPTION, MSG_EXCEPTION, list);
        }
    }

    /**
     * 新增团体信息
     */
    @ResponseBody
    @RequestMapping("add")
    public String add(HttpServletRequest httpRequest, HttpServletResponse response, Company company) {
        StandardDataFormat format = new StandardDataFormat(CODE_ERROR, MSG_ERROR, null);
        try {
            // 检查参数是否有误
            if (StringUtils.isBlank(company.getCompanyName())) {
                return HttpUtils.formatData(format, CODE_ERROR_PARAMS, MSG_ERROR_PARAMS, null);
            }
            // 字符转换
            company.setCompanyName(transString(httpRequest, company.getCompanyName()));
            company.setCompanyBrie(transString(httpRequest, company.getCompanyBrie()));
            // 检查是否已存在相同名称的团体
            List<Company> sames = companyService.selectSameCompany(company);
            if (sames != null && !sames.isEmpty()) {
                return HttpUtils.formatData(format, CODE_ERROR_SAME, MSG_ERROR_SAME, null);
            }
            // 添加团体信息
            boolean success = companyService.addCompany(company);
            if (success) {
                return HttpUtils.formatData(format, CODE_OK, MSG_OK, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            LoggerUtils.fmtError(getClass(), "添加团体信息异常%s", e.getMessage());
            return HttpUtils.formatData(format, CODE_EXCEPTION, MSG_EXCEPTION, null);
        }
        return HttpUtils.formatData(format, CODE_ERROR, MSG_ERROR, null);
    }

    /**
     * 修改团体信息
     */
    @ResponseBody
    @RequestMapping("update")
    public String update(HttpServletRequest httpRequest, HttpServletResponse response, Company company) {
        StandardDataFormat format = new StandardDataFormat(CODE_ERROR, MSG_ERROR, null);
        try {
            // 检查参数是否有误
            if (company == null || StringUtils.isBlank(company.getCompanyName()) || company.getCompanyId() == null) {
                return HttpUtils.formatData(format, CODE_ERROR_PARAMS, MSG_ERROR_PARAMS, null);
            }
            // 字符转换
            company.setCompanyName(transString(httpRequest, company.getCompanyName()));
            company.setCompanyBrie(transString(httpRequest, company.getCompanyBrie()));
            // 检查是否已存在相同名称的团体
            List<Company> sames = companyService.selectSameCompany(company);
            if (sames != null && !sames.isEmpty()) {
                return HttpUtils.formatData(format, CODE_ERROR_SAME, MSG_ERROR, null);
            }
            // 添加团体信息
            boolean success = companyService.updateCompany(company);
            if (success) {
                return HttpUtils.formatData(format, CODE_OK, MSG_OK, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            LoggerUtils.fmtError(getClass(), "修改团体信息异常：%s", e.getMessage());
            return HttpUtils.formatData(format, CODE_EXCEPTION, MSG_EXCEPTION, null);
        }
        return HttpUtils.formatData(format, CODE_ERROR, MSG_ERROR, null);
    }

    /**
     * 根据ID删除团体信息
     * 
     * @param companyId
     *            团体ID
     */
    @ResponseBody
    @RequestMapping("delete")
    public String delete(HttpServletRequest httpRequest, HttpServletResponse response, Integer companyId) {
        StandardDataFormat format = new StandardDataFormat(CODE_ERROR, MSG_ERROR, null);
        try {
            // 检查参数是否有误
            if (companyId == null) {
                return HttpUtils.formatData(format, CODE_ERROR_PARAMS, MSG_ERROR_PARAMS, null);
            }
            // 删除团体信息
            boolean success = companyService.deleteCompany(companyId);
            if (success) {
                return HttpUtils.formatData(format, CODE_OK, MSG_OK, null);
            }
        } catch (Exception e) {
            LoggerUtils.fmtError(getClass(), "删除团体信息异常%s", e.getMessage());
            return HttpUtils.formatData(format, CODE_EXCEPTION, MSG_EXCEPTION, null);
        }
        return HttpUtils.formatData(format, CODE_ERROR, MSG_ERROR, null);
    }
}
